package com.sugar.psych.assessment.module.business.service.userleavemessageliked;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.psych.assessment.module.business.controller.admin.userleavemessageliked.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.userleavemessageliked.UserLeaveMessageLikedDO;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;

import com.sugar.psych.assessment.module.business.dal.mysql.userleavemessageliked.UserLeaveMessageLikedMapper;

import static com.sugar.psych.assessment.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.psych.assessment.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.psych.assessment.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.psych.assessment.module.business.enums.ErrorCodeConstants.*;

/**
 * 用户留言点赞 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class UserLeaveMessageLikedServiceImpl implements UserLeaveMessageLikedService {

    @Resource
    private UserLeaveMessageLikedMapper userLeaveMessageLikedMapper;

    @Override
    public Long createUserLeaveMessageLiked(UserLeaveMessageLikedSaveReqVO createReqVO) {
        // 插入
        UserLeaveMessageLikedDO userLeaveMessageLiked = BeanUtils.toBean(createReqVO, UserLeaveMessageLikedDO.class);
        userLeaveMessageLikedMapper.insert(userLeaveMessageLiked);
        // 返回
        return userLeaveMessageLiked.getId();
    }

    @Override
    public void updateUserLeaveMessageLiked(UserLeaveMessageLikedSaveReqVO updateReqVO) {
        // 校验存在
        validateUserLeaveMessageLikedExists(updateReqVO.getId());
        // 更新
        UserLeaveMessageLikedDO updateObj = BeanUtils.toBean(updateReqVO, UserLeaveMessageLikedDO.class);
        userLeaveMessageLikedMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserLeaveMessageLiked(Long id) {
        // 校验存在
        validateUserLeaveMessageLikedExists(id);
        // 删除
        userLeaveMessageLikedMapper.deleteById(id);
    }

    @Override
        public void deleteUserLeaveMessageLikedListByIds(List<Long> ids) {
        // 校验存在
        validateUserLeaveMessageLikedExists(ids);
        // 删除
        userLeaveMessageLikedMapper.deleteByIds(ids);
        }

    private void validateUserLeaveMessageLikedExists(List<Long> ids) {
        List<UserLeaveMessageLikedDO> list = userLeaveMessageLikedMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(USER_LEAVE_MESSAGE_LIKED_NOT_EXISTS);
        }
    }

    private void validateUserLeaveMessageLikedExists(Long id) {
        if (userLeaveMessageLikedMapper.selectById(id) == null) {
            throw exception(USER_LEAVE_MESSAGE_LIKED_NOT_EXISTS);
        }
    }

    @Override
    public UserLeaveMessageLikedDO getUserLeaveMessageLiked(Long id) {
        return userLeaveMessageLikedMapper.selectById(id);
    }

    @Override
    public PageResult<UserLeaveMessageLikedDO> getUserLeaveMessageLikedPage(UserLeaveMessageLikedPageReqVO pageReqVO) {
        return userLeaveMessageLikedMapper.selectPage(pageReqVO);
    }

}